package controller;

import dao.impl.UserDaoImpl;
import entity.User;
import service.impl.UserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        UserDaoImpl userDaoImpl = new UserDaoImpl();
        try {
            User user = userDaoImpl.login(name,password);
            if (user == null){
                req.setAttribute("error","账号密码错误");
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/findAllPhone.do").forward(req,resp);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
